package beautiful.butterfly.schedule.test;

import beautiful.butterfly.schedule.strategy.Kind;
import beautiful.butterfly.schedule.strategy.ScheduleFactory;
import beautiful.butterfly.schedule.strategy.Strategy;
import beautiful.butterfly.schedule.task_manager.TaskType;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unitils.UnitilsJUnit4;
import org.unitils.spring.annotation.SpringApplicationContext;
import org.unitils.spring.annotation.SpringBeanByName;

@SpringApplicationContext({"schedule.xml"})
public class InitialDemoConfigData extends UnitilsJUnit4 {
    protected static transient Logger log = LoggerFactory
            .getLogger(InitialDemoConfigData.class);
    @SpringBeanByName
    ScheduleFactory scheduleFactory;

    public void setScheduleFactory(
            ScheduleFactory scheduleFactory) {
        this.scheduleFactory = scheduleFactory;
    }

    @Test
    public void initialConfigData() throws Exception {
        String baseTaskTypeName = "DemoTask";
        while (this.scheduleFactory.isZookeeperInitialSucess() == false) {
            Thread.sleep(1000);
        }
        scheduleFactory.stopStrategyTaskList(null);
        Thread.sleep(1000);
        try {
            this.scheduleFactory.getDataManager()
                    .deleteTaskType(baseTaskTypeName);
        } catch (Exception e) {

        }

        TaskType baseTaskType = new TaskType();
        baseTaskType.setBaseTaskType(baseTaskTypeName);
        baseTaskType.setDealBeanName("demoTaskBean");
        baseTaskType.setHeartBeatRate(2000);
        baseTaskType.setJudgeDeadInterval(10000);
        baseTaskType.setTaskParameter("AREA=哈哈,year>30");
        baseTaskType.setTaskItems(TaskType.splitTaskItem(
                "0:{TYPE=A,KIND=1},1:{TYPE=A,KIND=2},2:{TYPE=A,KIND=3},3:{TYPE=A,KIND=4}," +
                        "4:{TYPE=A,KIND=5},5:{TYPE=A,KIND=6},6:{TYPE=A,KIND=7},7:{TYPE=A,KIND=8}," +
                        "8:{TYPE=A,KIND=9},9:{TYPE=A,KIND=10}"));
        this.scheduleFactory.getDataManager()
                .createBaseTaskType(baseTaskType);
        log.info(":" + baseTaskType.toString());

        String taskName = baseTaskTypeName + "$TEST";
        String strategyName = baseTaskTypeName + "-Strategy";
        try {
            this.scheduleFactory.getManager()
                    .deleteStrategy(strategyName, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Strategy strategy = new Strategy();
        strategy.setStrategyName(strategyName);
        strategy.setKind(Kind.Schedule);
        strategy.setTaskName(taskName);
        strategy.setTaskParameter("哈哈");

        strategy.setNumOfSingleServer(1);
        strategy.setAssignNumber(10);
        strategy.setIpList("127.0.0.1".split(","));
        this.scheduleFactory.getManager()
                .createStrategy(strategy);
        log.info("" + strategy.toString());

    }
}
